package com.winexpress.session.support;

import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.CreateTopicRequest;
import com.amazonaws.services.sns.model.CreateTopicResult;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.PublishResult;
import com.amazonaws.services.sns.model.SubscribeRequest;
import com.amazonaws.services.sns.model.SubscribeResult;
import com.amazonaws.services.sns.model.SetTopicAttributesRequest;
import com.winexpress.entity.Customer;
import com.winexpress.session.account.CustomerSessionBean;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;

@Stateless
@LocalBean
public class NotificationSessionBean {

    @EJB
    private CustomerSessionBean customerSessionBean;
    private static final String EMAIL_PROTOCOL = "email";
    private static final String PROPERTY_PATH = "/simplejpa.properties";
    private static String snsArn = "arn:aws:sns:us-east-1:067519080133:WineXpress";

    private static AmazonSNSClient snsClient;

    static {
        AWSCredentials creds = new BasicAWSCredentials("AKIAICZHIWGI4CZ7D6IQ", "rFZNDkkY8YGim1ZIYymd6gfeUijUrgeJ20GCk9yp");
        snsClient = new AmazonSNSClient(creds);
    }

    public boolean createTopic() throws Exception {
        CreateTopicRequest request = new CreateTopicRequest();
        request.setName("test");
        CreateTopicResult result = snsClient.createTopic(request);
        snsArn = result.getTopicArn();

        this.addSubscribers();
        System.out.println("*************");
        System.out.println(snsArn);
        System.out.println("NotificationSessionBean: createTopic " + result.toString());
        return true;
    }

    private void addSubscribers() throws Exception {
        List<String> list = customerSessionBean.getCustomerSubscribeEmails();
        if (!list.isEmpty()) {
            Iterator it = list.iterator();
            while (it.hasNext()) {
                String s = (String) it.next();
                this.addSubscriber(s);
            }
        }
    }

    public boolean sendNotification(String title, String content) {
        if (title == null || content == null){
            return false;
        }
        
        SetTopicAttributesRequest r = new SetTopicAttributesRequest(snsArn, "DisplayName", "WineExpress");
        snsClient.setTopicAttributes(r);

        PublishRequest request = new PublishRequest();
        request.setTopicArn(snsArn);

        StringBuilder subject = new StringBuilder(title);
        request.setSubject(subject.toString());

        StringBuilder body = new StringBuilder(content);
        request.setMessage(body.toString());

        PublishResult result = snsClient.publish(request);
        System.out.println("NotificationSessionBean: sendNotification " + result.toString());

        return true;
    }

    public boolean addSubscriber(String email) {
        if (email == null){
            return false;
        }
        SubscribeRequest request = new SubscribeRequest(snsArn, EMAIL_PROTOCOL, email);
        SubscribeResult result = snsClient.subscribe(request);

        System.out.println("NotificationSessionBean: addSubscriber " + result.toString());
        return true;
    }

    public boolean deleteSubscriber(String email) {
        System.out.println("----------------");
        System.out.println(snsClient.listSubscriptions());
        return true;
    }

//    private static String getKey(){
//        PropertiesCredentials p = new PropertiesCredentials(ProductSessionBean.class.getResourceAsStream("AwsCredentials.properties"));
//        return p.getAWSSecretKey();
//    }
}
